package com.urfresh.sp.flume.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.ImmutableMap;
import com.urfresh.sp.flume.api.AppClientAPI;
import com.urfresh.sp.flume.avro.Client;
import com.urfresh.sp.flume.dto.Res;
import com.urfresh.sp.flume.dto.v2.SysInfo;
import com.urfresh.sp.flume.dto.v3.AppList;
import com.urfresh.sp.flume.dto.v3.DataInfos;
import com.urfresh.sp.flume.dto.v3.PageInfo;
import com.urfresh.sp.flume.dto.v3.PointInfo;
import com.urfresh.sp.flume.util.Util;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

/**
 * Created by urfresh.mark on 2016/11/15.
 */
@Api(value = "/v3/", description = "统一数据格式之后的埋点接口")
@EnableAsync
@RestController
@RequestMapping("v3")
public class AppClient implements AppClientAPI {

    Logger logger = LoggerFactory.getLogger(AppClient.class);

    @Autowired
    Client client;

    @Autowired
    Util util;

    @RequestMapping(value = "/applist", method = RequestMethod.POST)
    public Res applist(@Valid @RequestBody AppList dataInfos) {
        String value = JSON.toJSONString(dataInfos);
//        logger.info("用户操作数据：" + value);
        client.sendToFlume(value, "urfresh.track.app.applist");
        return Res.build("0");
    }

    @RequestMapping(value = "/info/{type}", method = RequestMethod.POST)
    public Res info(@PathVariable("type") String type,
                    @Valid @RequestBody DataInfos dataInfos) {
        dataInfos.setAction(type);

        logger.info(JSON.toJSONString(dataInfos));

        if (StringUtils.isEmpty(dataInfos.getPageJsonInfo()) == false) {
            String page = dataInfos.getPageJsonInfo();
            JSONObject po = JSON.parseObject(page);
            DataInfos d = new DataInfos();
            d.setAppVer(dataInfos.getAppVer());
            d.setRegionId(dataInfos.getRegionId());
            d.setRegionName(dataInfos.getRegionName());
            d.setStoreId(dataInfos.getStoreId());
            d.setStoreName(dataInfos.getStoreName());
            d.setAndroidId(dataInfos.getAndroidId());
            d.setAppId(dataInfos.getAppId());
            d.setIdfa(dataInfos.getIdfa());
            d.setSubmitTime(dataInfos.getSubmitTime());
            d.setUserName(dataInfos.getUserName());
            d.setAction(dataInfos.getAction());
            d.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSSS").format(new Date()));

            String elementId = po.containsKey("elementId") ? po.getString("elementId") : "";
            String action = po.containsKey("action") ? po.getString("action") : "";
            d.setElementId(elementId);
            d.setAction(action);
            
            String pageId = po.containsKey("pageId") ? po.getString("pageId") : "";
            String pageName = po.containsKey("pageName") ? po.getString("pageName") : "";

            String fPageId = po.containsKey("first_pageId") ? po.getString("first_pageId") : "";
            String fPageName = po.containsKey("first_pageName") ? po.getString("first_pageName") : "";
            String sPageId = po.containsKey("second_pageId") ? po.getString("second_pageId") : "";
            String sPageName = po.containsKey("second_pageName") ? po.getString("second_pageName") : "";

            String coordinate = po.containsKey("point_coordinate") ? po.getString("point_coordinate") : "";
            String pointId = po.containsKey("point_pointId") ? po.getString("point_pointId") : "";
            String pointName = po.containsKey("point_pointName") ? po.getString("point_pointName") : "";

            String gCoordinate = po.containsKey("group_coordinate") ? po.getString("group_coordinate") : "";
            String gPointId = po.containsKey("group_pointId") ? po.getString("group_pointId") : "";
            String gPointName = po.containsKey("group_pointName") ? po.getString("group_pointName") : "";

            if (StringUtils.isEmpty(pageId) == false)
                d.setPageInfo(Arrays.asList(new PageInfo(pageId, pageName)));
            else
                d.setPageInfo(Arrays.asList(new PageInfo(fPageId, fPageName), new PageInfo(sPageId, sPageName)));
            d.setPointInfo(new PointInfo(pointId, coordinate, pointName, new PointInfo(gPointId, gCoordinate, gPointName, null)));

            String value = JSON.toJSONString(d);
            logger.info("页面数据：" + value);
            client.sendToFlume(value, "urfresh.track.app.data");
//            }
        }
        String value = JSON.toJSONString(dataInfos);
        logger.info("用户操作数据：" + value);
        client.sendToFlume(value, "urfresh.track.app.data");
        return Res.build("0");
    }

    @RequestMapping(value = "/track", method = RequestMethod.POST)
    public Res track(HttpServletRequest request, @Valid @RequestBody SysInfo sysInfo) {
        String value = JSON.toJSONString(sysInfo);
        logger.info("app track info=" + value);
        client.sendToFlume(value, "urfresh.track.sys.data", ImmutableMap.of("type", "by_track", "ip", util.getIpAddr(request)));
        return Res.build("0");
    }

    @Override
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Res login(HttpServletRequest request, @Valid @RequestBody SysInfo sysInfo) {
        String value = JSON.toJSONString(sysInfo);
        logger.info("app user login=" + value);
        client.sendToFlume(value, "urfresh.track.sys.data", ImmutableMap.of("type", "login", "ip", util.getIpAddr(request)));
        return Res.build("0");
    }

    @Override
    @RequestMapping(value = "/sysInfo", method = RequestMethod.POST)
    public Res sysInfo(HttpServletRequest request, @Valid @RequestBody SysInfo sysInfo) {
        String value = JSON.toJSONString(sysInfo);
        logger.info("app start info=" + value);
        client.sendToFlume(value, "urfresh.track.sys.data", ImmutableMap.of("type", "start", "ip", util.getIpAddr(request)));
        return Res.build("0");
    }

    @Override
    @RequestMapping(value = "/sysInfo/{type}", method = RequestMethod.POST)
    public Res info(HttpServletRequest request, @PathVariable String type, @Valid @RequestBody SysInfo sysInfo) {
        String value = JSON.toJSONString(sysInfo);
        logger.info("app start info=" + value);
        client.sendToFlume(value, "urfresh.track.sys.data", ImmutableMap.of("type", type, "ip", util.getIpAddr(request)));
        return Res.build("0");
    }

}
